package org.chen.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Value("${db.slave.url}")
    private String url;

    @Value("${db.slave.username}")
    private String username;

    @Value("${db.slave.password}")
    private String password;

    @Value("${db.slave.driver-class-name}")
    private String driverClassName;

    @Bean(name = "slaveDataSource")
    public DataSource slaveDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setJdbcUrl(url + "/flyingorder?serverTimezone=Asia/Shanghai");
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name = "slaveDataSource1")
    public DataSource slaveDataSource1() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setJdbcUrl(url + "/flyingorder_1?serverTimezone=Asia/Shanghai");
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name = "slaveTemplate")
    public JdbcTemplate slaveTemplate(@Qualifier("slaveDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "slaveTemplate1")
    public JdbcTemplate slaveTemplate1(@Qualifier("slaveDataSource1") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
